package com.hxd.otc.module.wallet.presenter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseApplication;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.dialog.ConfirmValidTypeVerCodeDialog;
import com.hxd.otc.module.wallet.bean.BasePayBean;
import com.hxd.otc.module.wallet.bean.PayOrderResultBean;
import com.hxd.otc.module.wallet.contract.RemotePayContract;
import com.hxd.otc.module.wallet.model.PayModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.UIUtils;

/**
 * Created by lzy on 2018/8/22 0022.
 * description ${远程支付 Presenter}
 */
public class RemotePayPresenter extends BasePresenter<RemotePayContract.View> implements RemotePayContract.Presenter {

    private final PayModel mPayModel;
    private BasePayBean mBasePayBean;

    public RemotePayPresenter(RemotePayContract.View view) {
        super(view);
        mPayModel = PayModel.getInstance();
    }

    @Override
    public void setBasePayBean(BasePayBean basePayBean) {
        mBasePayBean = basePayBean;
    }

    @Override
    public void payOrder(String payPassword) {
        mPayModel.innerPayOrder(mBasePayBean.getPayInfoBean().getPreId(), payPassword, new BaseObserver<PayOrderResultBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull PayOrderResultBean payOrderResultBean) {
                mView.payOrderSuccess(payOrderResultBean);
            }

            @Override
            public void onNext(BaseBean<PayOrderResultBean> response) {
                if (response.getCode() == SUCCESS) {
                    String msg = response.getMsg();
                    if ("success".equals(msg)) {
                        msg = "";
                    }
                    onNext(msg, response.getData());
                } else if (response.getCode() == TOKEN_MISS || response.getCode() == TOKEN_NOT_MATCH) {
                    mView.onTokenMiss();
                }else if (response.getCode() == PAY_VALID_INVALID) {
                    Activity topActivity = BaseApplication.getInstance().getTopActivity();
                    if (topActivity == null) {
                        onError(response.getCode(), response.getMsg());
                    } else {
                        ConfirmValidTypeVerCodeDialog confirmValidTypeVerCodeDialog = new ConfirmValidTypeVerCodeDialog(topActivity);
                        confirmValidTypeVerCodeDialog.setCanceledOnTouchOutside(false);
                        confirmValidTypeVerCodeDialog.show();
                        onError(response.getCode(), "");
                    }
                } else if (response.getCode() == SERVER_UPDATING_CODE) {
                    String errMsg = response.getMsg();
                    errMsg = TextUtils.isEmpty(errMsg) ? ResourceUtil.getString(R.string.system_service_upgrade_in_progress) : errMsg;
                    Activity topActivity = BaseApplication.getInstance().getTopActivity();
                    if (topActivity == null) {
                        onError(response.getCode(), errMsg);
                    } else {
                        UIUtils.showServerUpdatingDialog(topActivity, errMsg);
                        onError(response.getCode(), "");
                    }
                } else {
                    onError(response.getCode(), response.getMsg());
                }
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.payOrderFail(errMessage);
            }
        },mView.<BaseBean<PayOrderResultBean>>bindAutoDispose());
    }
}
